package com.food.service.impl;

import com.food.model.UserProfile;
import com.food.model.dashboard.*;
import com.food.service.DashboardService;
import com.food.mapper.MealRecordMapper;
import com.food.mapper.UserGoalMapper;
import com.food.mapper.UserProfileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DashboardServiceImpl implements DashboardService {
    
    @Autowired
    private MealRecordMapper mealRecordMapper;
    
    @Autowired
    private UserGoalMapper userGoalMapper;
    
    @Autowired
    private UserProfileMapper userProfileMapper;
    
    @Override
    public DashboardResponse getDashboardData(Integer userId) {
        if (userId == null) {
            throw new RuntimeException("用户ID不能为空");
        }
        
        DashboardResponse response = new DashboardResponse();
        response.setDailyNutrition(getDailyNutrition(userId));
        response.setWeightTrend(getWeightTrend(userId));
        response.setGoalProgress(getGoalProgress(userId));
        response.setRecentMeals(getRecentMeals(userId));
        
        return response;
    }
    
    private DailyNutrition getDailyNutrition(Integer userId) {
        LocalDate today = LocalDate.now();
        DailyNutrition nutrition = mealRecordMapper.getDailyNutrition(userId, today);
        if (nutrition == null) {
            nutrition = new DailyNutrition();
            nutrition.setCalories(2100);
            nutrition.setProtein(75);
            nutrition.setCarbs(280);
            nutrition.setFat(70);
            nutrition.setCaloriesProgress(84);
            nutrition.setProteinProgress(94);
            nutrition.setCarbsProgress(93);
            nutrition.setFatProgress(108);
        }
        return nutrition;
    }
    
    private List<WeightRecord> getWeightTrend(Integer userId) {
        LocalDate endDate = LocalDate.now();
        LocalDate startDate = endDate.minusDays(30);
        
        List<WeightRecord> trend = userProfileMapper.getWeightTrend(userId, startDate, endDate);
        
        if (trend == null || trend.isEmpty()) {
            UserProfile profile = userProfileMapper.findByUserId(userId);
            WeightRecord record = new WeightRecord();
            record.setDate(LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE));
            record.setWeight(profile != null ? profile.getWeight() : 0.0);
            trend = Collections.singletonList(record);
        }
        
        return trend;
    }
    
    private GoalProgress getGoalProgress(Integer userId) {
        GoalProgress progress = userGoalMapper.getGoalProgress(userId);
        if (progress == null) {
            progress = new GoalProgress();
            progress.setType("减重目标");
            progress.setTarget("减重5kg");
            progress.setCurrent("已完成3kg");
            progress.setPercentage(60);
            progress.setStatus("success");
            progress.setStatusText("进行中");
        }
        return progress;
    }
    
    private List<DashboardMealRecord> getRecentMeals(Integer userId) {
        LocalDate today = LocalDate.now();
        List<DashboardMealRecord> meals = mealRecordMapper.getRecentMeals(userId, today);
        
        if (meals == null || meals.isEmpty()) {
            meals = new ArrayList<>();
            DashboardMealRecord meal = new DashboardMealRecord();
            meal.setId(1L);
            meal.setType("早餐");
            meal.setTime("08:00");
            meal.setDescription("全麦面包 + 牛奶 + 鸡蛋");
            meal.setCalories(350);
            meal.setProtein(15);
            meal.setCarbs(45);
            meal.setFat(12);
            meals.add(meal);
        }
        return meals;
    }
} 